﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;


namespace Marmat.Forms
{
    [ProvideProperty("PropertyName", typeof(Control))]
    [ProvideProperty("Id", typeof(Control))]
    public partial class Language : IExtenderProvider
    {
        private Dictionary<Control, string> m_PropertyName = new Dictionary<Control, string>();
        private Dictionary<Control, string> m_id = new Dictionary<Control, string>();

        public Control[] Controls
        {
            get
            {
                var list = new List<Control>();
                if (this.m_id is object)
                {
                    foreach (Control ct in this.m_id.Keys)
                        list.Add(ct);
                }

                return list.ToArray();
            }
        }

        [Browsable(true)]
        [DefaultValue("Text")]
        [Category("语言属性")]
        [DisplayName("PropertyName")]
        [Description("需要翻译的属性名称。")]
        public string GetPropertyName(Control Control)
        {
            if (this.m_PropertyName.ContainsKey(Control) == false)
            {
                return "Text";
            }

            string Value = this.m_PropertyName[Control];
            return Value;
        }

        public void SetPropertyName(Control control, string value)
        {
            if (!string.IsNullOrEmpty(value)) // 要保存配置
            {
                if (this.m_PropertyName.ContainsKey(control) == false)
                {
                    this.m_PropertyName.Add(control, value);
                }
                else
                {
                    this.m_PropertyName[control] = value;
                }
            }
            else
            {
                this.m_PropertyName.Remove(control);
            }
        }

        [Browsable(true)]
        [DefaultValue(null)]
        [Category("语言属性")]
        [DisplayName("Id")]
        [Description("翻译字典中对应的ID号。")]
        public string GetId(Control Control)
        {
            if (this.m_id.ContainsKey(Control) == false)
            {
                return null;
            }

            string Value = this.m_id[Control];
            return Value;
        }

        public void SetId(Control control, string value)
        {
            if (!string.IsNullOrEmpty(value)) // 要保存配置
            {
                if (this.m_id.ContainsKey(control) == false)
                {
                    this.m_id.Add(control, value);
                }
                else
                {
                    this.m_id[control] = value;
                }
            }
            else
            {
                this.m_id.Remove(control);
            }
        }

        public bool CanExtend(object extendee)
        {
            return true;
        }
    }
}